package io.awen.modules.sys.controller;

import io.awen.common.exception.RRException;
import io.awen.common.utils.FileUtils;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.R;
import io.awen.modules.sys.entity.ToolLocalStorage;
import io.awen.modules.sys.service.IToolLocalStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName ToolLocalStorageController
 * @Description 本地存储控制层接口
 * @Author gel
 * @Date 2020-10-31-15:46
 **/
@Api(tags = {"本地文件存储"})
@RestController
@RequestMapping("/sys/toolLocalStorage")
public class ToolLocalStorageController {

    @Autowired
    private IToolLocalStorageService toolLocalStorageService;

    /**
     * 分页
     *
     * @param params 分页参数
     * @return 分页对象
     */
    @ApiOperation("分页")
    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = toolLocalStorageService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 上传文件
     *
     * @param name          文件名
     * @param multipartFile 文件流
     * @return 本地存储信息
     */
    @ApiOperation("上传文件")
    @PostMapping("/save")
    public R create(@RequestParam(value = "name", required = false) String name, @RequestParam("file") MultipartFile multipartFile) {
        ToolLocalStorage toolLocalStorage = toolLocalStorageService.create(name, multipartFile);
        return R.ok().put("data", toolLocalStorage);
    }

    /**
     * 上传图片
     *
     * @param file 文件流
     * @return 本地存储信息
     */
    @PostMapping("/pictures")
    @ApiOperation("上传图片")
    public R upload(@RequestParam MultipartFile file) {
        // 判断文件是否为图片
        String suffix = FileUtils.getExtensionName(file.getOriginalFilename());
        if (!FileUtils.IMAGE.equals(FileUtils.getFileType(suffix))) {
            throw new RRException("只能上传图片");
        }
        ToolLocalStorage toolLocalStorage = toolLocalStorageService.create(null, file);
        return R.ok().put("data", toolLocalStorage);
    }

    /**
     * 下载
     *
     * @param storageId 本地存储id
     */
    @GetMapping("/download/{storageId}")
    @ApiOperation("下载")
    public void download(@PathVariable Long storageId) {
        toolLocalStorageService.download(storageId);
    }

    /**
     * 获取文件组信息
     *
     * @param storageIds 本地存储信息Ids
     * @return 是否成功
     */
    @ApiOperation("获取文件组信息")
    @PostMapping("/info")
    public R info(@RequestBody Set<Long> storageIds) {
        return R.ok().put("data", toolLocalStorageService.info(storageIds));
    }

    /**
     * 获取文件组信息
     *
     * @param ids 本地存储信息Ids
     * @return 是否成功
     */
    @ApiOperation("获取文件组信息")
    @PostMapping("/infolist")
    public R infoList(@RequestBody Set<Long> ids) {
        return R.ok().put("data", toolLocalStorageService.getfileList(ids));
    }

    /**
     * 修改
     *
     * @param toolLocalStorage 本地存储信息
     * @return 是否成功
     */
    @ApiOperation("修改")
    @PutMapping("/update")
    public R update(@RequestBody ToolLocalStorage toolLocalStorage) {
        toolLocalStorageService.update(toolLocalStorage);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param storageIds 本地存储信息Ids
     * @return 是否成功
     */
    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Set<Long> storageIds) {
        toolLocalStorageService.delete(storageIds);
        return R.ok();
    }
}
